<!-- 黑名单列表 -->
<template>
  <page-header-wrapper>
    <a-card class="table-card">
      <GzSearchForm :searchFunc="searchFunc" :resetFunc="clearFunc" searchConditionNum="6">
        <a-form-item class="table-search-item" label="规则名称">
          <a-select v-model:value="searchData['blacklistRuleId']" placeholder="规则名称" @change="handleChange">
            <a-select-option v-for="(item,index) in ruleNameList" :key="index" :value="item.blacklistRuleId">{{ item.ruleName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item class="table-search-item" label="手机号">
          <a-input v-model:value="searchData['phone']" placeholder="请输入手机号" />
        </a-form-item>
        <a-form-item class="table-search-item" label="唯一标识">
          <a-input v-model:value="searchData['cstmNo']" placeholder="请输入唯一标识" />
        </a-form-item>
      </GzSearchForm>
       
      <!-- 列表渲染 -->
      <GzTable
        ref="infoTableRef"
        :init-data="true"
        :req-table-data-func="reqTableDataFunc"
        :table-columns="tableColumns"
        :search-data="searchData"
      >
        <template #topBtnSlot>
          <GzUploadExe v-if="$access('ENT_LIMT_CONDITION_BLACK_INFO_IMPT') && ruleType==1" :showUploadList="false" :isNeedImport="false" @sendData="importBlackList">
            <template #uploadBtn>
              <a-button type="primary">
                <plus-outlined />导入黑名单
              </a-button>
            </template>
          </GzUploadExe>
          <a-button v-if="$access('ENT_LIMT_CONDITION_BLACK_INFO_DOWN')" type="primary" @click="downloadBlackList">
            <export-outlined />下载模版
          </a-button>
          <a-button v-if="$access('ENT_LIMT_CONDITION_BLACK_INFO_EXPT')" type="primary" :disabled="total == 0" @click="exportBlackList">
            <export-outlined />导出黑名单
          </a-button>
        </template>
  
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'ruleType'">{{ record.ruleType === 1 ? '其他' : '客户经理' }}</template>
          <template v-if="column.key === 'operation'">
            <a-button v-if="$access('ENT_LIMT_CONDITION_BLACK_INFO_DEL')" type="link" @click="deleteFunc(record.blacklistId)">删除</a-button>
          </template>
        </template>
      </GzTable>
    </a-card>
  </page-header-wrapper>
</template>
  
  <script lang="ts" setup>
  import { req, API_URL_BLACKLISTRULE_GETALLRULE, API_URL_BLACKLIST, API_URL_BLACKLIST_REMOVE,$exportExcel ,API_URL_EXPORT_BLACKLIST,API_URL_IMPORT_BLACKLIST} from '@/api/manage'
  import { ref, reactive, getCurrentInstance,onMounted ,watch} from 'vue'
  import * as XLSX from 'xlsx'
  import fileDownload from 'js-file-download'
  
  const { $infoBox, $access } = getCurrentInstance()!.appContext.config.globalProperties
  
  const infoTableRef = ref()

  
  let tableColumns = ref([]) as any // 列表表头
  
  const searchData:any = ref({})
  function searchFunc() {
    infoTableRef.value.refTable(true)
  }
  const total = ref(0)
  function clearFunc(){
    searchData.value = {}
   
  }
  const ruleNameList = ref([]) as any
// 获取所有规则列表
  async  function getAllRuleList() {
  if(ruleNameList.value.length > 0){
    return
  }
  const  res =  await req.list(API_URL_BLACKLISTRULE_GETALLRULE,{})
    ruleNameList.value = res
}


  // 请求table接口数据
  function reqTableDataFunc(params: any) { 
  // 如果没有选择规则，则不请求数据
  if(!searchData.value.blacklistRuleId){
    return new Promise((resolve) => {
      total.value = 0
      resolve({ data: [], total: 0 })
      // 提示请选择规则
      $infoBox.message.warning('请选择规则')
    })
  }else{
        return new Promise(async (resolve, reject) => {
     req.list(API_URL_BLACKLIST, params)
      .then((res) => {
        total.value = res.total
        resolve(res)
      })
  })
  }
  }
  const  blacklistRuleId = ref('') as  any
  const ruleType = ref('') as any
  function handleChange(value:any) {
     blacklistRuleId.value = value
    // 根据规则id遍历找出来ruleNameList对应的相同对象
     let obj = ruleNameList.value.find(item => item.blacklistRuleId === value)
     ruleType.value = obj.ruleType
    tableColumns.value =obj.columnNameList

}

// 删除
function deleteFunc(blacklistId){
  $infoBox.confirmDanger('确认删除？', '请确认是否删除当前规则？', () => {
    req.add(API_URL_BLACKLIST_REMOVE,{ blacklistId }).then(res => {
      $infoBox.message.success('删除成功！')
      infoTableRef.value.refTable(true)
    })
  })
}
// excel表格导出函数数据
const exportToExcel = (item, name) => {
  // 创建一个工作簿
  const workbook = XLSX.utils.book_new()

  // 创建一个工作表
  const worksheet = XLSX.utils.aoa_to_sheet([...item])

  // 将工作表添加到工作簿
  XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1')

  // 生成Excel文件
  XLSX.writeFile(workbook, `${name}.xlsx`)
}

function downloadBlackList(){
  let tempalteInformation: any = undefined
  let tempalteName: any
  tempalteName = '导入黑名单模板'
  tempalteInformation = []
  exportToExcel(tempalteInformation, tempalteName)
}
// 导入黑名单
const  importBlackList =  (ossFileUrl)=>{
     req.add(API_URL_IMPORT_BLACKLIST, { ossFileUrl,blacklistRuleId:blacklistRuleId.value }).then(res => { 
      infoTableRef.value.refTable(true)
    })
}


// 导出黑名单列表
function exportBlackList(){
  $exportExcel(API_URL_EXPORT_BLACKLIST,{ blacklistRuleId:blacklistRuleId.value },'POST').then(res => {    
    fileDownload(res.data,'黑名单列表.xlsx')
  })
}
onMounted(()=>{
  getAllRuleList()
})
  </script>
  